package com.fhz.day004

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.fhz.day004.adapter.FoodAdapter
import com.fhz.day004.contract.FoodContract
import com.fhz.day004.entity.Data
import com.fhz.day004.model.FoodModel
import com.fhz.day004.plus.toast
import com.fhz.day004.presenter.FoodPresenter
import com.fhz.day004.utils.LogUtil
import com.fhz.day004.view.BaseActivity
import com.orhanobut.logger.Logger
import com.orhanobut.logger.Logger.d
import com.orhanobut.logger.Logger.i
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<FoodPresenter>(),FoodContract.IFoodView ,View.OnClickListener{


    private lateinit var adapter: FoodAdapter
    private lateinit var list: MutableList<Data>
    override fun bindLayoutId(): Int = R.layout.activity_main

    override fun initView() {

        requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        ,android.Manifest.permission.READ_EXTERNAL_STORAGE),100)

        rv.layoutManager = LinearLayoutManager(this)
        list = mutableListOf()
        adapter = FoodAdapter(R.layout.rv_layout, list)
        rv.adapter = adapter


        //点击事件的问题
        adapter.onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener{ _, view, position ->
            when(view.id){
                R.id.rv_img -> {
                    position.toast(this)
                }
                R.id.rv_title ->{
                    "asdf".toast(this)
                }
            }
        }

        btn.setOnClickListener(this)

//        btn.setOnClickListener{
//            "123".toast(this)
//        }

    }
        override fun initData() {
//            Log.v("initData","初始化完成")
//            Log.d("initData","初始化完成")
//            Log.i("initData","初始化完成")
//            Log.w("initData","初始化完成")
//            Log.e("initData","初始化完成")

            LogUtil.v("initData","初始化完成")
            LogUtil.d("initData","初始化完成")
            LogUtil.i("initData","初始化完成")
            LogUtil.w("initData","初始化完成")
            LogUtil.e("initData","初始化完成")

            d("hello")



            p = FoodPresenter(FoodModel(), this)
            p.getData()


        }

        override fun initAdapter(foods: MutableList<Data>) {
            list.addAll(foods)
            adapter.notifyDataSetChanged()
            Log.d("initAdapter","适配器初始化完成")
        }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btn -> {
                "11".toast(this)
                i("init","点我")
            }
        }
    }

//    fun btn(view: View) {
////        "adf".toast(this)
//    }
}
